﻿using UnityEngine;
using UnityEngine.UI;

public class StartState : ISceneState
{
    private Image mImage;
    private float speed = 0.1f;
    private float totalTime;
    private float waitTime;

    public StartState(SceneStateController controller) : base("01StartScene", controller)
    {
    }

    private void loadStartImage()
    {
        mImage = GameObject.Find("Logo").GetComponent<Image>();
        mImage.color = Color.black;
        // 目标是在 3 秒内完成插值
        waitTime = 1f; // 秒
        var desiredCompletion = 0.99f; // 插值接近完成的程度
        // 根据公式计算速度
        speed = -Mathf.Log(1 - desiredCompletion) / waitTime;
    }

    public override void StateStart()
    {
        loadStartImage();
    }

    public override void StateUpdate()
    {
        mImage.color = Color.Lerp(mImage.color, Color.white, speed * Time.deltaTime);
        // 累加 Time.deltaTime，更新总时长
        totalTime += Time.deltaTime;
        if (totalTime >= waitTime)
        {
            mController.isRunStart = false;
            mController.SetState(new MainMenuState(mController));
        }
    }
}